<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class shop extends core {

	public function __construct() {
		parent::__construct();
	}

	//
	public function index() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT * 
			FROM jf_shop
			WHERE status='1' ORDER BY sort ASC, id DESC 
			LIMIT {$offset}, {$pagesize}";
		$shops = $this->db->get_results($sql);
		if ($shops) {
			foreach ($shops as $key => $shop) {
				//$shops[$key]->comment_time = date("Y-m-d H:i:s", $shop->comment_time);
			}
			$shops = add_domain_deep($shops, array("poster_pic"));
		}
		success($shops);
	}

	//详情
	public function detail() {
		$id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT * FROM jf_shop WHERE id='{$id}' AND status='1'";
		//echo $sql;
		$shop = $this->db->get_row($sql);
		$shop = add_domain_deep($shop, array("poster_pic"));
		if ($shop) {
			$sql = "SELECT * FROM jf_ad WHERE status='1' AND shop_id='{$id}' 
				ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";	
			$ads = $this->db->get_results($sql);
			$ads = add_domain_deep($ads, array("pic"));
			$shop->ads = $ads;
			$sql = "UPDATE jf_shop SET clicks=clicks+1 WHERE id='{$id}'";
			$this->db->query($sql);
		}
		success($shop);
	}

	//门店地图, 微信端用
	public function maper() {
		$this->detail();
	}
	
	//门店地图
	//不需要读取接口，直接从上一页里获取，然后呈现在wap网页
	public function map() {
		$longitude = (isset($_GET['longitude']) && trim($_GET['longitude'])) ? floatval($_GET['longitude']) : 0;
		$latitude = (isset($_GET['latitude']) && trim($_GET['latitude'])) ? floatval($_GET['latitude']) : 0;
		success(array("longitude"=>$longitude, "latitude"=>$latitude));
	}

}
